package sim;

import java.awt.Graphics;

import javax.swing.JPanel;

public class DiscreteHistoPanel extends JPanel{
	
	long[] heights;
	long stride;
	int binCount;
	int offset;
	int xLength;
	int yLength;
	int drawStrideX;
	int drawStrideY;
	
	public DiscreteHistoPanel(long[] counts, long stride, int binCount, int xLength, int yLength) {
		this.offset = 5;
		this.xLength = xLength;
		this.yLength = yLength;
		int length = counts.length;
		this.stride = stride;
		this.binCount = binCount;
		this.heights = new long[length];
		this.drawStrideX = xLength / binCount;
		calcVals(counts, stride);
		long max = getMax(heights);
		if(max == 0){
			this.drawStrideY = (int) 1;
		}
		else{
			this.drawStrideY = (int) (yLength / max);
		}
	}
	
	private long getMax(long[] array){
		long acc = 0;
		for (int i = 0; i < array.length; i++) {
			acc = array[i] > acc ? array[i] : acc;
		}
		return acc;
	}
	
	private void calcVals(long[] counts, long stride){
		int length = counts.length;
		for (int i = 0; i < length; i++) {
			long height = counts[i] / stride;
			heights[i] = height == 0 ? 1 : height;
		}
	}
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		drawAxis(g);
	}
	
	private void drawAxis(Graphics g){
		g.drawLine(offset, offset, xLength + offset, offset);
		g.drawLine(offset, offset, offset, yLength + offset);
		for (int i = 0; i < binCount - 1; i++) {
			g.drawRect(offset + (i * drawStrideX), offset, offset + ((i + 1) * drawStrideX), offset + (int) heights[i] * drawStrideY);
		}
	}

}
